package com.yjt.fresh.activity;

import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiChangePhone;
import com.yjt.fresh.api.ApiGetConfirmCode;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanCity;
import com.yjt.fresh.bean.BeanUserInfo;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToast;
import com.yjt.fresh.widget.CustomToastResult;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by UPC on 2018/8/20.
 */

public class AtyChangeBindPhoneNext extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.etAtyChangePhonePhoneNum)
    EditText mEtPhoneNum;
    @BindView(R.id.btnAtyChangePhoneConfirm)
    Button mBtnConfirm;
    @BindView(R.id.edtAtyChangePhoneConfirmCode)
    EditText mEdtConfirmCode;
    @BindView(R.id.btnAtyChangePhoneNext)
    Button mBtNext;


    @Override
    protected int getContentView() {
        return R.layout.app_aty_change_bind_phone_next;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mEdtConfirmCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0) {
                    mBtNext.setClickable(true);
                    mBtNext.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                } else {
                    mBtNext.setClickable(false);
                    mBtNext.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mEtPhoneNum.getText().toString()) && CommonsToolsHelper.isPhoneNum(mEtPhoneNum.getText().toString())) {
                    getConfirmCode();
                } else {
                    Toast.makeText(mContext, "不是合法的手机号码", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mBtNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                checkConfirmCode();
            }
        });

        mBtNext.setClickable(false);
    }

    private void checkConfirmCode() {
        ApiChangePhone apiChangePhone = new ApiChangePhone(new HttpOnNextListener<BeanCity>() {
            @Override
            public void onReLogin() {

            }

            @Override
            public void onNext(BeanCity beanCity) {
                CustomToastResult.show("换绑成功");
                BeanUserInfo userInfo = PreferenceManager.getInstance().getUserInfo();
                userInfo.setPHONE(mEtPhoneNum.getText().toString());
                PreferenceManager.getInstance().setUserInfo(userInfo);
                finish();
            }

            @Override
            public void failed(String massage) {
                super.failed(massage);
                Toast.makeText(mContext, massage, Toast.LENGTH_SHORT).show();
            }
        }, mContext, true, false, true);
        Map<String, Object> map = new HashMap<>();
        map.put("YZM", mEdtConfirmCode.getText().toString().trim());
        map.put("PHONE", mEtPhoneNum.getText().toString());
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiChangePhone.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiChangePhone);
    }

    HttpOnNextListener<BaseResultEntity> mConfirmCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            Toast.makeText(mContext, "验证码已发送", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void getConfirmCode() {
        new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                mBtnConfirm.setEnabled(false);
                mBtnConfirm.setText("获取验证码" + l / 1000);
                mBtnConfirm.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
            }

            @Override
            public void onFinish() {
                mBtnConfirm.setEnabled(true);
                mBtnConfirm.setText("获取验证码");
                mBtnConfirm.setTextColor(ContextCompat.getColor(mContext, R.color.color_009944));
            }
        }.start();
        ApiGetConfirmCode apiGetConfirmCode = new ApiGetConfirmCode(mConfirmCodeListener, mContext, false, false, true);
        Map<String, Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("PHONE", mEtPhoneNum.getText().toString());
        map.put("MESSAGE_TYPE", 3);//3是绑定手机号码
        apiGetConfirmCode.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetConfirmCode);
    }

}
